<template>
  <div class="container-fluid">
    <div class="row school-model school-tiao">
      <div class="col school-tiao-left">
        <span>{{ $route.name }}</span>
      </div>
      <div class="col text-right">
        <a-button-group>
          <a-button type="primary">
            <router-link class="text-white" :to="{name: '布置作业'}">布置作业</router-link>
          </a-button>
        </a-button-group>
      </div>
    </div>
    <div class="row school-model index-model">
      <div class="col school-col-table">
        <el-table
          :data="tableData.slice((currentPage - 1) * pagesize, currentPage * pagesize)"
          style="width: 100%; border: 1px solid #eeeeee"
        >
          <el-table-column prop="ID" label="序号" width="60">
            <template slot-scope="scope">{{scope.$index+1}}</template>
          </el-table-column>
          <el-table-column prop="schoolclazzs" label="班级/学员" width="180">
            <template
              slot-scope="scope"
            >{{ getschoolclazz.filter(v=> scope.row.schoolclazzs?scope.row.schoolclazzs.includes(v.ID):'').map(v=> v.name).join(',') }}</template>
          </el-table-column>
          <el-table-column prop="title" width="120" label="作业标题"></el-table-column>
          <el-table-column prop="type" sortable width="120" label="类型"></el-table-column>
          <el-table-column prop="info" label="接收率">
            <template slot-scope="scope">
              <router-link
                v-show="scope.row.type!=='草稿'"
                :to="{name: '作业提交详情', params: { ID: scope.row.ID }}"
              >{{ scope.row.schoolstudenthomework.filter(v=> ['已接收', '已提交', '已点评'].includes(v.type) ).length }}/{{scope.row.schoolstudenthomework.length}}</router-link>
            </template>
          </el-table-column>
          <el-table-column prop="studentnum" label="提交率">
            <template slot-scope="scope">
              <router-link
                v-show="scope.row.type!=='草稿'"
                :to="{name: '作业提交详情', params: { ID: scope.row.ID }}"
              >{{ scope.row.schoolstudenthomework.filter(v=> [ '已提交', '已点评'].includes(v.type)).length }}/{{scope.row.schoolstudenthomework.length}}</router-link>
            </template>
          </el-table-column>
          <el-table-column prop="type" label="点评率" width="80px">
            <template slot-scope="scope">
              <router-link
                v-show="scope.row.type!=='草稿'"
                :to="{name: '作业提交详情', params: { ID: scope.row.ID }}"
              >{{ scope.row.schoolstudenthomework.filter(v=> v.type == '已点评').length }}/{{scope.row.schoolstudenthomework.length}}</router-link>
            </template>
          </el-table-column>
          <el-table-column prop="type" label="未读" width="160px">
            <template slot-scope="scope">
              <router-link
                :to="{name: '作业提交详情', params: { ID: scope.row.ID }}"
              >{{ reply(scope.row) }}</router-link>
            </template>
          </el-table-column>
          <el-table-column prop="studentnum" sortable label="布置教师" width="120px">
            <template slot-scope="scope">{{ getschooluserbyid(scope.row.uid).name }}</template>
          </el-table-column>
          <el-table-column prop="studentnum" sortable label="布置日期" width="120px">
            <template slot-scope="scope">{{ totime(scope.row.createdAt) }}</template>
          </el-table-column>
          <el-table-column align="right" width="220px">
            <template slot-scope="scope">
              <el-button type="primary" v-show="scope.row.type === '草稿'" size="small">
                <router-link class="text-white" :to="{name: '布置作业', query: {ID: scope.row.ID}}">编辑</router-link>
              </el-button>
              <el-button type="danger" size="small" @click="del(scope.row.ID)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          small
          @current-change="currentChange"
          :page-size="pagesize"
          layout="total, prev, pager, next"
          :total="tableData.length"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { asyncdelschoolhomework } from "../../../../plugins/api.js";
import { totime } from "../../../utils.js";
export default {
  name: "homework",
  data() {
    return {
      search: "",
      currentPage: 1,
      pagesize: 10
    };
  },
  computed: {
    ...mapGetters([
      "getschooluser",
      "getschoolhomework",
      "getschoolclazz",
      "getschooluserbyid"
    ]),
    tableData() {
      const { getschoolhomework, mname } = this;
      return getschoolhomework.filter(data => {
        return mname ? data.name.includes(mname) : true;
      });
    }
  },
  methods: {
    totime,
    currentChange(index) {
      this.currentPage = index;
    },
    del(ID) {
      this.$confirm("此操作将永久删除该作业, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          try {
            await asyncdelschoolhomework({ ID });
            await this.$store.dispatch("actionschoolhomework");
            this.$message.success("删除成功");
          } catch (err) {
            this.$message.error("删除失败");
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    reply({ schoolhomeworkitembank }) {
      let d = 0;
      for (let v of schoolhomeworkitembank) {
        d += v.schoolhomeworkitembankreply.filter(
          v => v.createuser === "student" && !v.state
        ).length;
      }
      return d;
    }
  }
};
</script>

<style lang="scss" scoped>
.index-model {
  margin-top: 13px;
}
.dialog-footer {
  text-align: center;
}
</style>